import {defineStore} from 'pinia'
import {RequestData, Role} from "@/api/role/type.ts";
import {getRoleList} from "@/api/role";
import {MenuItem} from "@/api/menu/type.ts";
import {buildMenuTree} from "@/utils/util.ts";


export const useRolesStore = defineStore('roles', () => {
    const roleList = reactive<Role[]>([]);
    const openEdit = ref(false);
    const openAdd = ref(false);
    const roleId = ref(0);
    const menuList = reactive<MenuItem[]>([])
    const filteredData = reactive({
        name: '',
        isUsable: 3,
    })
    const page_size = reactive([10, 20, 30, 50])
    const reqData = reactive<RequestData>(
        {
            name: filteredData.name,
            isUsable: filteredData.isUsable,
            pageSize: page_size[0],
            currentPage: 1,
        }
    )
    const getRoleListFun = async () => {
        const res = await getRoleList(reqData);
        if (res.code !== 200) return Promise.reject(res.msg);
        roleList.length = 0;
        roleList.push(...res.data.roleList);
        menuList.length = 0;
        menuList.push(...buildMenuTree(res.data.menuList))
        return Promise.resolve(res.data.total)
    }
    return {
        roleList,
        getRoleListFun,
        openEdit,
        roleId,
        menuList,
        filteredData,
        reqData,
        page_size,
        openAdd
    }
})

